//
//  WPTitleView.m
//  wobingwoyi
//
//  Created by WGP on 16/7/27.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "WPTitleView.h"
#import "WPBtnNoHighLight.h"

#define TAG_BASE 1000
//背景绿颜色
#define WPGreenColor RGB_COLOR(3, 181, 229)

@interface WPTitleView ()

@property(nonatomic,strong)WPBtnNoHighLight *individualCasesBtn;
@property(nonatomic,strong)WPBtnNoHighLight *medicalTreatmentBtn;
@property(nonatomic,strong)UIButton *currentSelectedBtn;
@property(nonatomic,strong)UIView *sliderView;
@property(nonatomic,assign)CGFloat margin;
@property(nonatomic,copy)void(^handleBlock)(WPReadingTitleViewType titleBtnType);

@property(nonatomic,assign)WPReadingTitleViewType btnType;

@end

@implementation WPTitleView

-(instancetype)initWithDidClickBlock:(void(^)(WPReadingTitleViewType titleBtnType))handleBlock
{
    if (self = [super init]) {
        
        self.handleBlock = handleBlock;
        
        [self setupUI];
    }
    return self;
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        
        [self setupUI];
    }
    return self;
}

-(void)setupUI
{
    self.backgroundColor = GlobalColor;
    
    WPBtnNoHighLight *individualCasesBtn = [self buttonWithTitle:@"普通病例" tag:TAG_BASE];
    self.individualCasesBtn = individualCasesBtn;
    self.individualCasesBtn.selected = YES;
    self.currentSelectedBtn = self.individualCasesBtn;
    
    WPBtnNoHighLight *medicalTreatmentBtn = [self buttonWithTitle:@"精华病例" tag:TAG_BASE+1];
    self.medicalTreatmentBtn = medicalTreatmentBtn;
    
    UIView *bottomLine = [[UIView alloc] init];
    bottomLine.backgroundColor = RGB_SAME(220);
    [self addSubview:bottomLine];
    
    self.sliderView = [[UIView alloc] init];
    self.sliderView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.sliderView];
    
    [individualCasesBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(self.margin);
        make.top.equalTo(self).offset(28);
    }];
    
    [medicalTreatmentBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.individualCasesBtn);
        make.right.equalTo(self).offset(-self.margin);
    }];
    
    [self.sliderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(individualCasesBtn);
        make.height.mas_equalTo(1.5);
        make.centerY.equalTo(self.mas_bottom).offset(-3.5);
        make.centerX.equalTo(self.individualCasesBtn);
    }];
    
}

-(WPBtnNoHighLight *)buttonWithTitle:(NSString *)title tag:(NSInteger)tag
{
    //MAXFLOAT最大值--没有限制
    CGSize maxSize=CGSizeMake(MAXFLOAT, MAXFLOAT);
    //设置用于计算度高的字体大小
    NSDictionary *att=@{NSFontAttributeName:[UIFont systemFontOfSize:15]};
    CGSize actSize=[title boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:att context:nil].size;
    self.margin = (SCREEN_WIDTH-2*actSize.width)/3.0;
    
    WPBtnNoHighLight *btn = [[WPBtnNoHighLight alloc] init];
    btn.tag = tag;
    [btn setTitle:title forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:15];
    [btn sizeToFit];
    [self addSubview:btn];
    [btn setTitleColor:RGB_COLOR(237, 237, 235) forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    return btn;
}

-(void)btnDidClick:(UIButton *)btn
{
    
    
    if (self.currentSelectedBtn == btn) {
        return ;
    }
    
    self.currentSelectedBtn.selected = NO;
    self.currentSelectedBtn = btn;
    self.currentSelectedBtn.selected = YES;
    
    [self moveSliderViewWith:btn.tag];
    
    if (self.handleBlock) {
        self.handleBlock(self.btnType);
    }
}

-(void)moveSliderViewWith:(NSInteger)index
{
    if (index == TAG_BASE) {
        
        self.btnType = WPReadingTitleViewTypeIndival;
        
        [self.sliderView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.individualCasesBtn);
        }];
        
    }else
    {
        self.btnType = WPReadingTitleViewTypeFamous;
        
        [self.sliderView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.individualCasesBtn).offset(self.margin+60);
        }];
    }
    
    [UIView animateWithDuration:0.25 animations:^{
        [self layoutIfNeeded];
    }];
    
    
}

-(void)moveSliderWith:(WPReadingTitleViewType )titleBtnType
{

    if (titleBtnType == WPReadingTitleViewTypeIndival) {
        [self btnDidClick:self.individualCasesBtn];
    }else
    {
        [self btnDidClick:self.medicalTreatmentBtn];
    }
}

-(void)scrollBtnDidClick:(UIButton *)btn
{
    
    
    if (self.currentSelectedBtn == btn) {
        return ;
    }
    
    self.currentSelectedBtn.selected = NO;
    self.currentSelectedBtn = btn;
    self.currentSelectedBtn.selected = YES;
    
    [self moveSliderViewWith:btn.tag];
}

@end
